var KnowledgeCategoryPage = {
	// 知识分类JavaBean需要的字段
	CATEGORY_STRUCTURE:[
	    'category_id','category_name','status',
	    'create_user','parentId','categoryWeight'
	],
	// 新增
	add:function(){
		var newNode = categoryTree.addNode(categoryTree.getCurrentNode());
		if (newNode) {
			newNode.status='1';
		}
		$("#editCategoryFrm").autofill(newNode);
		KnowledgeCategoryPage.listTags(newNode.category_group, newNode['category_id']);
	},
	// 删除
	deleteNode:function(){
		var currNode = categoryTree.getCurrentNode();
		// Logger.info("delete node => " + JSON.stringify(currNode));
		categoryTree.removeNode(currNode);
		$("#editCategoryFrm")[0].reset();
	},
	// 保存
	save:function(){
		var data = categoryTree.getRecorder().pack(this.CATEGORY_STRUCTURE);
		// Logger.info("category 正在准备数据：" + JSON.stringify(data));
		saveCategoryAjax.setData($.toJSON(data));
		saveCategoryAjax.send();
	},
	// 保存成功
	onSaveAllOperateSuccess:function(data, status, xhr) {
		MsgBox.info(data.message);
		categoryTree.getRecorder().clearAll(); // 保存成功后，需要将先前的操作清空
	},
	// 验证表单
	validate:function(currNode) {
		if (StringUtil.isBlank(currNode.category_name)) {
			$.messager.error('类别名称不能为空！');
			$("#editCategoryFrm input[name='category_name']").focus();
			return false;
		}
		if (StringUtil.lengthGreaterThan(currNode.category_name, 60)) {
			$.messager.error('类别名称太长，超出了60个字符');
			$("#editCategoryFrm input[name='category_name']").focus();
			return false;
		}
		if (StringUtil.lengthGreaterThan(currNode.categoryWeight, 1)) {
			$.messager.error("类别权重在0-9之间");
			$("#editCategoryFrm input[name='categoryWeight']").focus();
			return false;
		}
		return true;
	},
	// 分类名称发生改变
	onValueChanged:function(ele) {
		var currNode = categoryTree.getCurrentNode();
		// Logger.info('正在修改节点：' + JSON.stringify(currNode));
		if (!currNode) {
			return;
		}
		var obj = $("#editCategoryFrm").serializeObject();
		if (this.validate(obj)){
			$.extend(currNode, obj);
			categoryTree.updateNode(currNode, true);
		}
	},
	// 刷新知识分类树
	refresh:function(){
		queryAllCategoryAjax.send();
	},
	onCategoryItemSelected:function(){
		var node = categoryTree.getCurrentNode();
		// Logger.info('category node = ' + JSON.stringify(node));
		$("#editCategoryFrm").autofill(node);
		KnowledgeCategoryPage.listTags(node.category_group, node['category_id']);
	},
	listTags:function(data, categoryId) {
		$("#editCategoryFrm").find('.t-res-group').listTags({
			'contentDefine':{
				'id':'group_id',
				'text':'group_name'
			},
			'data':data,
			'afterBuild':function($container) {
				var $edit= $("<a href='javascript:void(0);' class='t-res-group-edit'><i class='icon icon-edit'/></a>");
				$edit.attr({
					'data-id':categoryId
				});
				$edit.on('click',function(e){
					e.stopPropagation();
					e.preventDefault();
					KnowledgeCategoryPage.openEditCategoryGroupDialog($(this));
				});
				$container.append($edit);
			},
			'css':{
				'border':'none'
			},
			'emptyText':function(){
				return "该知识分类目前没有资源组，点击右上角的编辑按钮<i class='icon icon-edit'></i>可以添加资源组";
			}
		});
	},
	// 打开编辑分组对话框
	openEditCategoryGroupDialog:function($edit) {
		if (categoryTree.getRecorder().hasOperate()) {
			MsgBox.confirm("你修改了知识类型树，但没有保存，是否立即保存", null, function(){
				KnowledgeCategoryPage.save();
			});
			return ;
		}
		
		var categoryId = $edit.data('id');
		var url = ctx + '/sms/categoryManage/categroupEdit/index/' + categoryId;
		MsgBox.open('editCategoryGroup', '编辑资源分组', url,'1000px');
	},
	// 查询数据成功回调
	onQueryCategorySuccess:function(data, status, xhr) {
		categoryTree.expandAll();
		// categoryTree.selectFirstRootNode();
	},
	// 刷新资源组
	refreshCategoryGroup:function() {
		var node = categoryTree.getCurrentNode();
		var ourl = queryCategoryGroupXByIdAjax.getUrl();
		var url = queryCategoryGroupXByIdAjax.getUrl() + node.category_id;
		queryCategoryGroupXByIdAjax.setUrl(url);
		queryCategoryGroupXByIdAjax.send();
		queryCategoryGroupXByIdAjax.setUrl(ourl); // 恢复原有url
	},
	onQueryCategoryGroupSuccess:function(data, status, xhr) {
		var node = categoryTree.getCurrentNode();
		node.category_group = data.info;
		KnowledgeCategoryPage.listTags(node.category_group, node['category_id']);
	}
};

/* 20170601-lj-改树形结构
function onRenderItem(colName,text){
	switch(colName){
	case '':
		var html = '<div>'+
				'<button class="btn kbtn editbtn">编辑</button>'+
				'<button class="btn kbtn tableDelbtn mobtn">删除</button>'+
			'</div>';
		var html = '<div class="tableRowsIcon">'+
						'<span title="修改" style="margin-right:0.7em"><a class="editbtnA" style="color:#bd7b46;" href="javascript:;">'+
							'<i class="icon icon-edit"></i>'+
						'</a></span>'+
						'<span title="删除"><a class="mobtn" href="javascript:;" style="color:#bd7b46;">'+
						'<i class="icon icon-trash"></i>'+
					'</a></span>'+
				'</div>';
		return html;
	case 'status':
		var txt = (text == '0' || text == 0)?"禁用":"正常";
		var color = (text == '0' || text == 0)?"red":"green";
		return '<span style="color:'+color+'">'+txt+'</span>';
	}
}
function onAfterReady(){
	$(".editbtnA").click(function(e){
		var num = $(this).parents('tr').index();
		var id = categoryLstTable.rows[num].data[0];
		MsgBox.open("categoryCreate","修改分类",
				ctx+"/sms/categoryManage/create/true/"+id,'','','lg');
		e.stopPropagation();
		e.preventDefault();
	});
	$(".mobtn").click(function(e){
		var num = $(this).parents('tr').index();
		var record = categoryLstTable.rows[num];
		MsgBox.confirm("确认删除分类【"+record.data[1].text+"】？","删除确认",function(){
			var id = record.data[0];
			var arr = new Array();
			arr.push(id);
			$("#delIds").val(id);
			delCategoriesAjax.send();
		},null);
		e.stopPropagation();
		e.preventDefault();
	});
}
function onDelSuccess(data){
	if(data.success == true){
		new $.zui.Messager('删除成功!', {
		    type: 'success',
		    time: 2000,
		}).show();
		categoryLstTable.refresh();
	}
}
function onAddCategory(){
	MsgBox.open("categoryCreate","新增分类",ctx+"/sms/categoryManage/create/false/''",
			'','','lg');
}

function onBatchDel(){
	var selectRecords = categoryLstTable.getSelectedRecords();
	if(selectRecords == null || selectRecords.length == 0){
		MsgBox.warn("请选择至少一条记录","消息提示",null);
	}else{
		MsgBox.confirm("确认删除所选【"+selectRecords.length+"】条分类信息？","删除确认",function(){
			var ids = new Array();
			for(var i=0;i<selectRecords.length;i++){
				ids.push(selectRecords[i].data.category_id);
			}
			$("#delIds").val(ids);
			delCategoriesAjax.send();
		},null);
	}
}
function onRefresh(){
	categoryLstTable.refresh();
}
var extra = {
		showLoading:function(){
			$("#msg_dialog_loading").show();
		},
		closeLoading:function(){
			$("#msg_dialog_loading").hide();
		}
}*/